package com.ruoyi.web;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.MyLoginService;
import com.ruoyi.framework.web.service.MyTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Null;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@RestController
public class MyloginViewController  {
    @Autowired
    private MyLoginService myLoginService;
    @Autowired
    private MyTokenService myTokenService;
    @RequestMapping("/mylogin")
    public AjaxResult mylogin( @RequestParam String loginphone, @RequestParam String loginpwd, @RequestParam String loginrule){

       String msg= myLoginService.login(loginphone,loginrule,loginpwd);


       if(msg.equals("请进行注册")||msg.equals("密码错误")||msg.equals("登录出现了非HR和seeker用户"))
       {
           return new AjaxResult(1,msg);}
       else{
          /* try {
               response.setCharacterEncoding("UTF-8");
               response.setHeader("Access-Control-Allow-Credentials", "true");
               //设置允许多个域名允许跨域集合
               String[] allowDomains = {"http://localhost,http://localhost/frontindex", "http://localhost/static/html/index.html"};
               Set allowOrigins = new HashSet(Arrays.asList(allowDomains));
               String origin = request.getHeader("Origin");
               if (allowOrigins.contains(origin)) {
                   //设置允许跨域的配置:Access-Control-Allow-Origin： 响应头指定了该响应的资源是否被允许与给定的origin共享
                   response.setHeader("Access-Control-Allow-Origin", origin);
               }
               //获取cookie
               Cookie[] cookies = request.getCookies();
               if(cookies!= null){
                   System.out.println("cookie不为空");
               }
               /*
               //设置cookie
               Cookie cookie1 = new Cookie("usertoken", msg);
               cookie1.setPath("/");
               response.addCookie(cookie1);
           }
                catch (Exception e) {
               e.printStackTrace();
           }*/
           return new AjaxResult(200,msg);
       }
    }
    //返回当前登录用户的角色和id
    @RequestMapping("/mycheckloginuser")
    public AjaxResult mycheckloginuser(HttpServletRequest request,HttpServletResponse response){

       /* String token = request.getParameter("token");
        if(token.equals("notoken")){
            System.out.println("没有登录");
            return new AjaxResult(1,"请先进行登录");
        }
        else {
            System.out.println("有登录用户"+token);
            String loginrule = myTokenService.getloginrole(token);
            Long ruleid = myTokenService.getloginid(token);
            return new AjaxResult(200,loginrule,ruleid);
        }*/
        return myLoginService.getroleandid(request,response);

    }
    //返回当前登录用户姓名("/mylogingetusername")
    @RequestMapping("/mylogingetusername")
    public AjaxResult mylogingetusername(HttpServletRequest request,HttpServletResponse response) {
    return myLoginService.getrolename(request,response);
    }
}
